<template>
  <div id="aCoursesList" class="bg-fa of">
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac"><span class="c-333">全部课程</span></h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt><span class="c-999 fsize14">课程类别</span></dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a href="#" @click.prevent="searchAll()" :class="{active: oneIndex === -1}">全部</a>
                </li>
                <li v-for="(item, index) in subjectNestedList" :key="item.id" @click.prevent="searchOne(item.id, index)"
                    :class="{active: oneIndex === index}">
                  <a href="#" :title="item.title">{{ item.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt><span class="c-999 fsize14"></span></dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(item, index) in subSubjectList" :key="item.id" @click.prevent="searchTwo(item.id, index)"
                    :class="{active2: twoIndex === index}">
                  <a href="#" :title="item.title">{{ item.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">{{ pageData.current }}</i> /
              <i class="c-666 f-fM">{{ pageData.pages }}</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange': buyCountSort !== ''}">
                <a href="#" title="销量" @click.prevent="searchBuyCount()">销量
                  <span v-if="buyCountSort !== ''" :class="{hide: buyCountSort === '1'}">↑</span>
                  <span v-if="buyCountSort !== ''" :class="{hide: buyCountSort === '2'}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange': gmtCreateSort !== ''}">
                <a href="#" title="最新" @click.prevent="searchGmtCreate()">最新
                  <span v-if="gmtCreateSort !== ''" :class="{hide: gmtCreateSort === '1'}">↑</span>
                  <span v-if="gmtCreateSort !== ''" :class="{hide: gmtCreateSort === '2'}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange': priceSort !== ''}">
                <a href="#" title="价格" @click.prevent="searchPrice()">价格&nbsp;
                  <span v-if="priceSort !== ''" :class="{hide: priceSort === '1'}">↑</span>
                  <span v-if="priceSort !== ''" :class="{hide: priceSort === '2'}">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>

        <div class="mt40">
          <section v-if="pageData.total === 0" class="no-data-wrap">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，鼠鼠正在努力整理中...</span>
          </section>
          <article v-if="pageData.total > 0" class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="item in pageData.records" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title"/>
                    <div class="cc-mask">
                      <a :href="`/course/${item.id}`" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="getCourseDetailLink(item.id)" :title="item.title" class="course-title fsize18 c-333">{{
                        item.title
                      }}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span v-if="Number(item.price) === 0" class="fr jgTag bg-green">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span v-else class="fr jgTag bg-green">
                      <i class="c-fff fsize12 f-fA">￥{{ item.price }}</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ item.buyCount }}人学习</i> |
                      <i class="c-999 f-fA">{{ item.commentCount || 0 }}评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>

        <div class="paging">
          <a href="#" :class="{undisable: !pageData.hasPrevious}" title="首页" @click.prevent="gotoPage(1)">首</a>
          <a href="#" :class="{undisable: !pageData.hasPrevious}" title="前一页"
             @click.prevent="gotoPage(pageData.current - 1)">&lt;</a>
          <a v-for="page in pageData.pages" :key="page"
             :class="{current: pageData.current === page, undisable: pageData.current === page}"
             href="#" @click.prevent="gotoPage(page)">
            {{ page }}
          </a>
          <a href="#" :class="{undisable: !pageData.hasNext}" title="后一页"
             @click.prevent="gotoPage(pageData.current + 1)">&gt;</a>
          <a href="#" :class="{undisable: !pageData.hasNext}" title="末页"
             @click.prevent="gotoPage(pageData.pages)">末</a>
          <div class="clear"></div>
        </div>
      </section>
    </section>
  </div>
</template>

<script>
import courseApi from '@/api/course'

export default {
  data() {
    return {
      pageData: {
        current: 1,
        pages: 0,
        total: 0,
        hasPrevious: false,
        hasNext: false,
        records: []
      },
      subjectNestedList: [],
      subSubjectList: [],
      searchObj: {},
      oneIndex: -1,
      twoIndex: -1,
      buyCountSort: '',
      gmtCreateSort: '',
      priceSort: ''
    }
  },
  created() {
    this.fetchSubjects()
    this.fetchCourses(1)
  },
  methods: {
    getCourseDetailLink(courseId) {
      return `/course/${courseId}`
    },
    fetchSubjects() {
      courseApi.getAllSubject().then(res => {
        if (res.data && Array.isArray(res.data.data)) {
          this.subjectNestedList = res.data.data
          this.subSubjectList = []
          this.subjectNestedList.forEach(item => {
            if (item.children && Array.isArray(item.children)) {
              this.subSubjectList.push(...item.children)
            }
          })
        }
      }).catch(() => {
        this.subjectNestedList = []
        this.subSubjectList = []
      })
    },
    fetchCourses(page) {
      courseApi.getCourseList(page, 8, this.searchObj).then(res => {
        if (res.data && res.data.data) {
          // console.log(`当前数据： ${res.data.data.records}`)
          // alert(res.data.data.records)
          this.pageData = res.data.data
          console.log(`当前数据： ${this.pageData}`)
        }
      }).catch(() => {
        this.pageData = {
          current: 1,
          pages: 0,
          total: 0,
          hasPrevious: false,
          hasNext: false,
          items: []
        }
      })
    },
    gotoPage(page) {
      if (page < 1 || page > this.pageData.pages) return
      this.fetchCourses(page)
    },
    searchAll() {
      this.oneIndex = -1
      this.twoIndex = -1
      this.searchObj = {}
      this.subSubjectList = []
      this.subjectNestedList.forEach(item => {
        if (item.children && Array.isArray(item.children)) {
          this.subSubjectList.push(...item.children)
        }
      })
      this.gotoPage(1)
    },
    searchOne(subjectParentId, index) {
      this.oneIndex = index
      this.twoIndex = -1
      this.searchObj = {
        ...this.searchObj,
        subjectParentId,
        subjectId: ''
      }
      const selected = this.subjectNestedList.find(s => s.id === subjectParentId)
      this.subSubjectList = selected && selected.children ? selected.children : []
      this.gotoPage(1)
    },
    searchTwo(subjectId, index) {
      this.twoIndex = index
      this.searchObj = {
        ...this.searchObj,
        subjectId
      }
      this.gotoPage(1)
    },
    resetSort() {
      this.buyCountSort = ''
      this.gmtCreateSort = ''
      this.priceSort = ''
    },
    searchBuyCount() {
      this.buyCountSort = this.buyCountSort === '1' ? '2' : '1'
      this.gmtCreateSort = ''
      this.priceSort = ''
      this.searchObj = {
        ...this.searchObj,
        buyCountSort: this.buyCountSort,
        gmtCreateSort: '',
        priceSort: ''
      }
      this.gotoPage(1)
    },
    searchGmtCreate() {
      this.gmtCreateSort = this.gmtCreateSort === '1' ? '2' : '1'
      this.buyCountSort = ''
      this.priceSort = ''
      this.searchObj = {
        ...this.searchObj,
        buyCountSort: '',
        gmtCreateSort: this.gmtCreateSort,
        priceSort: ''
      }
      this.gotoPage(1)
    },
    searchPrice() {
      this.priceSort = this.priceSort === '1' ? '2' : '1'
      this.buyCountSort = ''
      this.gmtCreateSort = ''
      this.searchObj = {
        ...this.searchObj,
        buyCountSort: '',
        gmtCreateSort: '',
        priceSort: this.priceSort
      }
      this.gotoPage(1)
    }
  }
}
</script>

<style scoped>
.active {
  background: #66b1ff;
  color: white;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 3px;
}

.active2 {
  background: #66b1ff;
  color: white;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 3px;
}

.hide {
  display: none;
}

.clear {
  clear: both;
}
</style>
